package deviceService

import (
	"errors"
	"fmt"
	"github.com/spf13/cast"
	"hc_server/pkg/channel"
	"hc_server/routers/models"
	"strings"
	"time"
)

type Device struct {
	Id int

	DeviceSn      string     // 设备sn
	DeviceModelId int        // 设备型号 device_models主键id
	DeviceTUSN    string     // 设备TUSN
	StockStatus   string     // 库存状态：0=待出库 1=已出库
	StockTime     *time.Time //入库时间
	BindingStatus string     // 绑定状态：0=未绑定 1=已绑定
	BindingTime   *time.Time // 绑定时间
	BaseAgentId   int        // 直属下级id
	AgentId       int        // 所属代理商id
	MerchantId    int        // 所属商户id

	PageNum  int
	PageSize int
	Keyword  string

	SnFrom   string //设备sn 开始区间
	SnTo     string //设备sn 结束区间
	CodeFrom string //收款码 开始区间
	CodeTo   string //收款码 结束区间
}

// DeviceInfoAdd 添加设备信息
func (d *Device) DeviceInfoAdd() error {
	//获取设备型号信息
	exist, err := models.ExistDeviceModelById(d.DeviceModelId)
	if err != nil || !exist {
		return errors.New("设备型号不存在")
	}

	relation, err := models.AgentRelationGetInfoByAgentNo(d.AgentId)
	if err != nil {
		return err
	}

	return models.DeviceInfoAdd(models.Device{
		DeviceSn:      d.DeviceSn,
		DeviceModelId: d.DeviceModelId,
		DeviceTUSN:    d.DeviceTUSN,
		AgentId:       d.AgentId,
		BaseAgentId:   cast.ToInt(relation["baseAgentUid"]),
	})
}

// DeviceInfoSearch 查询设备信息
func (d *Device) DeviceInfoSearch() (map[string]interface{}, error) {
	return models.DeviceInfoSearch(d.AgentId, d.PageNum, d.PageSize, d.Keyword)
}

// DeviceInfoList 设备信息列表
func (d *Device) DeviceInfoList() (map[string]interface{}, error) {
	return models.DeviceInfoList(d.AgentId, d.PageNum, d.PageSize)
}

// DeviceInfo 获取设备信息
func (d *Device) DeviceInfo() (interface{}, error) {
	return models.DeviceInfo(d.Id)
}

// DeviceAllocateInfoList 获取可划拨设备列表
func (d *Device) DeviceAllocateInfoList() (map[string]interface{}, error) {
	return models.DeviceAllocateInfoList(d.AgentId, d.DeviceModelId, d.PageNum, d.PageSize)
}

// DeviceRecallInfoList 获取可召回设备列表
func (d *Device) DeviceRecallInfoList() (map[string]interface{}, error) {
	return models.DeviceRecallInfoList(d.AgentId, d.DeviceModelId, d.PageNum, d.PageSize)
}

// DeviceRecallAgentList 获取可召回设备代理列表
func (d *Device) DeviceRecallAgentList() (map[string]interface{}, error) {
	return models.DeviceRecallAgentList(d.AgentId, d.DeviceModelId, d.PageNum, d.PageSize)
}

// DeviceInfoListSearch 设备列表搜索
func (d *Device) DeviceInfoListSearch() (map[string]interface{}, error) {
	var condition []string
	condition = append(condition, fmt.Sprintf("agent_id = %d", d.AgentId))
	if d.SnFrom != "" {
		condition = append(condition, fmt.Sprintf("device_sn >= '%s'", d.SnFrom))
	}
	if d.SnTo != "" {
		condition = append(condition, fmt.Sprintf("device_sn <= '%s'", d.SnTo))
	}
	if d.CodeFrom != "" {
		condition = append(condition, fmt.Sprintf("payment_code >= '%s'", d.CodeFrom))
	}
	if d.CodeTo != "" {
		condition = append(condition, fmt.Sprintf("payment_code <= '%s'", d.CodeTo))
	}
	if d.StockStatus != "" {
		condition = append(condition, fmt.Sprintf("stock_status = %s", d.StockStatus))
	}
	if d.BindingStatus != "" {
		condition = append(condition, fmt.Sprintf("binding_status = %s", d.BindingStatus))
	}
	if !d.StockTime.IsZero() {
		condition = append(condition, fmt.Sprintf("created_at >= '%s'", d.StockTime))
	}
	if !d.BindingTime.IsZero() {
		condition = append(condition, fmt.Sprintf("binding_time >= '%s'", d.BindingTime))
	}
	if d.MerchantId != 0 {
		condition = append(condition, fmt.Sprintf("merchant_id = %d", d.MerchantId))
	}
	return models.DeviceInfoListSearch(d.DeviceModelId, d.PageNum, d.PageSize, strings.Join(condition, " AND "))
}

// MerchantDeviceBind 设备绑定/解绑
func (d *Device) MerchantDeviceBind() (map[string]interface{}, error) {
	result, err := channel.LianDeviceBind(&channel.LianDeviceBindParam{
		BindType:   d.BindingStatus,
		MerchantId: cast.ToString(d.MerchantId),
		DeviceSn:   d.DeviceSn,
	})
	if err != nil {
		return nil, err
	}
	err = models.DeviceMerchantAdd(d.MerchantId, d.DeviceSn)
	if err != nil {
		return nil, err
	}
	return result, nil
}
